package com.admin.education.controllers;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.exceptions.UnauthorizedException;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@ResponseBody
public class ExceptionAdvise {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public IResponseMessage handleException(MethodArgumentNotValidException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Exception,e.getMessage());
        return responseMessage;
    }
    @ExceptionHandler(BadRequestException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public IResponseMessage handleException(BadRequestException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Error,e.getMessage());
        return responseMessage;
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public IResponseMessage handleException(UnauthorizedException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Error,e.getMessage());
        return responseMessage;
    }
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public IResponseMessage handleException(ExpiredJwtException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Error,e.getMessage());
        return responseMessage;
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public IResponseMessage handleException(HttpMessageNotReadableException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Error,e.getMessage());
        return responseMessage;
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public IResponseMessage handleException(ResourceNotFoundException e) {
        IResponseMessage responseMessage = new ResponseMessage(Result.Error,e.getMessage());
        return responseMessage;
    }
}
